<?php
namespace Core\CoreContracts;

use Core;

require_once (CONTRACTS_DIR . '/statesevent.enum.php');
/**
* Event model definition
*
* @method Create($name, $title, $description, $eventDate)
* @method Plan()
* @method CloseSubscriptions()
* @method Run()
* @method Cancel()
* @method Done()
*/
class Event {

	/**
    * @property integer $Id The event unique identifier
    */
	public $Id;
	
	/**
    * @property int $State The state event
    */
	public $State;
	
	/**
    * @property string The event title
    */
	public $Title;
	
	/**
    * @property string $Description The event description
    */
	public $Description;
	
	/**
    * @property datetime $StartDate The start date event
    */
	public $StartDate;
	
	/**
    * @property datetime $StartTime The start time event
    */
	public $StartTime;
	
	/**
    * @property datetime $EndDate The end date event
    */
	public $EndDate;
	
	/**
    * @property datetime $EndTime The end time event
    */
	public $EndTime;
	
	/**
    * @property string $PictureMime The picture mime of the event
    */
	public $PictureMime;
	
	/**
    * @property string $PictureExtension The picture extension of the event
    */
	public $PictureExtension;
	
	/**
    * @property blob $Picture The picture of the event
    */
	public $Picture;
	
	/**
    * @property int $NumberOfPlaces The number of places
    */
	public $NumberOfPlaces;
	
	/**
    * @property string $ArchiveName The folder archive name
    */
	public $ArchiveName;
	
	/**
    * @property string $Location The event location (google maps url)
    */
	public $Location;
	
	/**
    * @property bool $IsSubscriptionValidationRequired The subscription validation is required for event subscription
    */
	public $IsSubscriptionValidationRequired;
	
	/**
    * @property datetime $ModificationDate The event modification date
    */
	public $ModificationDate;
	
	/**
    * @property datetime $CreationDate The event creation date
    */
	public $CreationDate;
	
	/**
	* The default constructor
	*/
	public function __construct()  {  }
	
	/**
	* Build a new event object
	*
	* @param string $title The event title 
	* @param string $description The event description 
	* @param datetime $startDate The start event date 
	* @param datetime $endDate The end event date 
	* @param integer $numberOfPlaces The number of places (-1 to set infinite places)
	* @param string $location The event location
	*
	* @return Return a new event object
	*/
	public static function Create($title, $description, $startDate, $startTime, $endDate, $endTime, $numberOfPlaces, $location, $isSubscriptionValidationRequired)
	{
		if($startDate > $endDate)
		{
			throw new \Exception("La date de début doit être avant la date de fin.");
		}
	
		$event = new Core\CoreContracts\Event();
		$event->Title = $title;
		$event->Description = $description;
		$event->StartDate = $startDate;
		$event->StartTime = $startTime;
		$event->EndDate = $endDate;
		$event->EndTime = $endTime;
		$event->NumberOfPlaces = $numberOfPlaces;
		$event->Location = $location;
		$event->IsSubscriptionValidationRequired = $isSubscriptionValidationRequired;
		$event->Edit();
		$event->CreationDate = date("Y-m-d H:i:s");
		return $event;
	}

	/*
	* Change state event to edit
	*/
	public function Edit()
	{
		if($this->State == StateEvent::Running)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_RunCannotGoOnEdit"));
		}
		
		if($this->State == StateEvent::Done)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_DoneCannotGoOnEdit"));
		}
		
		$this->State = StateEvent::Edit;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	* Change state event to planned
	*/
	public function Plan()
	{
		if($this->State == StateEvent::Running)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_RunCannotGoOnPlan"));
		}
		
		if($this->State == StateEvent::Done)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_DoneCannotGoOnPlan"));
		}
		
		$this->State = StateEvent::Planned;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	* Change state event to close subscriptions
	*/
	public function CloseSubscriptions()
	{
		if($this->State == StateEvent::Running)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_RunCannotGoOnCloseSubscriptions"));
		}

		$this->State = StateEvent::CloseSubscriptions;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	* Change state event to run
	*/
	public function Run()
	{
		if($this->State == StateEvent::Cancelled)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_CancelCannotGoOnRun"));
		}

		$this->State = StateEvent::Running;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	* Change state event to cancel
	*/
	public function Cancel()
	{
		if($this->State == StateEvent::Running)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_RunCannotGoOnCancel"));
		}
		
		if($this->State == StateEvent::Done)
		{
			throw new \Exception(Core\CoreCommons\Localization::Get("Events_Exception_DoneCannotGoOnCancel"));
		}

		$this->State = StateEvent::Cancelled;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	* Change state event to done
	*/
	public function Done()
	{
		$this->State = StateEvent::Done;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
}
?> 